//
//  SettingViewController.m
//  DriftDog
//
//  Created by rimi on 15/9/8.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "SettingViewController.h"
#import "CustomSettingsTableViewCell.h"
#import "PersonalInfoDetailViewController.h"
#import "ProductIntroduceViewController.h"
#import "ContuctUsViewController.h"

static NSString *kUITableViewCellIdentifier = @"cellIdentifier";

@interface SettingViewController () <UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *_dataSource;
}

@property (nonatomic, strong) UITableView *tableView;

- (void)initializeDataSource;
- (void)initializeUserInterface;
 
@end

@implementation SettingViewController

- (void)viewDidLoad {
    
    /* 设置cell之间的线的顶穿 */
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    /* xx */
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}

#pragma mark - init

- (void)initializeDataSource {

    NSArray *leftImageName = @[@"个人资料图标.png", @"产品简介图标.png", @"评价建议图标.png", @"联系我们图标.png", @"分享我们图标.png", @"退出登录图标.png"];
    NSArray *titleName = @[@"个人资料", @"产品简介", @"评价建议", @"联系我们", @"分享我们", @"退出登录"];
    _dataSource = @{@"leftImageName":leftImageName, @"titleName":titleName};  // 初始化数据源
}

- (void)initializeUserInterface {

    self.view.backgroundColor = [UIColor colorWithWhite:236 / 255.f alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    /* 添加父类控件 */
    [self addTitleLabelWithName:@"设置"];
    [self addBaseLeftButtonWithImage:[UIImage imageNamed:@"返回图标"]];
    [self.baseLeftButton addTarget:self action:@selector(respondsToBaseLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    
    /* 添加子类控件 */
    [self.view addSubview:self.tableView];
    
}

#pragma mark - responds event

- (void)respondsToBaseLeftButton:(UIButton *)sender {

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

/* 设置cell之间的线的顶穿 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

// section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, ORIGIN_WIDTH, 5), NO)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}

// section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 8;
}

// 被点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; // cell不选中的时候颜色
    
    if (indexPath.section == 0) {        // --> 个人资料
        
        PersonalInfoDetailViewController *personalDetail_vc = [[PersonalInfoDetailViewController alloc] init];
        [self.navigationController pushViewController:personalDetail_vc animated:YES];
    }else if (indexPath.section == 1) {
    
        if (indexPath.row == 0) {        // --> 产品简介
            
            ProductIntroduceViewController *productIntroduce_vc = [[ProductIntroduceViewController alloc] init];
            [self.navigationController pushViewController:productIntroduce_vc animated:YES];
        }else if (indexPath.row == 1) {  // --> 评价建议
        
            
        }
    }else if (indexPath.section == 2) {
    
        if (indexPath.row == 0) {        // --> 联系我们
            
            ContuctUsViewController *contuctUs_vc = [[ContuctUsViewController alloc] init];
            [self.navigationController pushViewController:contuctUs_vc animated:YES];
        }else if (indexPath.row == 1) {  // --> 分享我们
        
        
        }else if (indexPath.row == 2) {  // -->退出登录
        
            
        }
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        
        return 1;
    }else if (section == 1) {
    
        return 2;
    }else if (section == 2) {
    
        return 3;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    
    if (!cell) {
        
        cell = [[CustomSettingsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIdentifier];
    }
    // 配置cell
    if (indexPath.section == 0) { // 个人资料 有
        
        cell.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[@"leftImageName"][0]]]; // 这种要好一点
        cell.titleLabel.text = _dataSource[@"titleName"][0];
        cell.rightIndicator.hidden = NO;
    }else if (indexPath.section == 1) {
    
        if (indexPath.row == 0) { // 产品简介 有
            
            cell.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[@"leftImageName"][1]]];
            cell.titleLabel.text = _dataSource[@"titleName"][1];
            cell.rightIndicator.hidden = NO;
        }else if (indexPath.row == 1) { // 评价建议 有
        
            cell.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[@"leftImageName"][2]]];
            cell.titleLabel.text = _dataSource[@"titleName"][2];
            cell.rightIndicator.hidden = NO;
        }
    }else if (indexPath.section == 2) {
    
        if (indexPath.row == 0) { // 联系我们 有指示器
            
            cell.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[@"leftImageName"][3]]]; // 这种要好一点
            cell.titleLabel.text = _dataSource[@"titleName"][3];
            cell.rightIndicator.hidden = NO;
        }else if (indexPath.row == 1) { // 分享我们 没有
        
            cell.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[@"leftImageName"][4]]]; // 这种要好一点
            cell.titleLabel.text = _dataSource[@"titleName"][4];
            cell.rightIndicator.hidden = YES;
        }else if (indexPath.row == 2) { // 退出登录
        
            cell.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[@"leftImageName"][5]]]; // 这种要好一点
            cell.titleLabel.text = _dataSource[@"titleName"][5];
            cell.rightIndicator.hidden = YES;
        }
    }
    
    return cell;
}

#pragma mark - getter

- (UITableView *)tableView {

    if (!_tableView) {

        _tableView = [[UITableView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 75, ORIGIN_WIDTH, ORIGIN_HEIGHT - 75), NO) style:UITableViewStylePlain];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 70;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor colorWithWhite:236 / 255.f alpha:1];
        _tableView.bounces = NO;
    }
    return _tableView;
}

@end
